Read and write using Streams and Java data types

    
  

Java Code
Screen Image
Important Points
Download Source Code

Read and write an array of Java 'Strings' to persistent storage.
ByteArrayInputStream and DataInputStream are used for reading.
ByteArrayOutputStream and DataOutputStream are used for writing.

The following array show the values written to and read from the record store:

String[] strings = {"Snow", "Rain", "Sleet", "Wind", "Hail"};     

Java Code:

/*--------------------------------------------------
* RWstreams.java
*
* Use streams to read and write Java data types
*-------------------------------------------------*/
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;

public class RWstreams extends MIDlet
{
  private RecordStore rs = null;    // Record store
  static final String RECORD_STORE = "rmstest"; // Name of record store

  public RWstreams()
  {
    openRMS();    // Open/create the record store
    
    writeRMS();   // Write data
    readRMS();    // Read data
        
    closeRMS();   // Close record store
    deleteRMS();  // Delete the record store
  }

  public void destroyApp( boolean unconditional )
  {
  }

  public void startApp()
  {
    // No user interface, ok to shutdown
    destroyApp(false);
    notifyDestroyed();
  }

  public void pauseApp()
  {
  }

  public void openRMS()
  {
    try
    {
      rs = RecordStore.openRecordStore(RECORD_STORE, true );
    }
    catch (Exception e)
    {
      db(e.toString());
    }
  }    
  
  public void closeRMS()
  {
    try
    {
      rs.closeRecordStore();
    }
    catch (Exception e)
    {
      db(e.toString());
    }
  }

  public void deleteRMS()
  {
    if (RecordStore.listRecordStores() != null)
    {
      try
      {
        RecordStore.deleteRecordStore(RECORD_STORE);
      }
      catch (Exception e)
      {
        db(e.toString());
      }
    }      
  }

  /*--------------------------------------------------
  * Create three arrays to write to record store
  *-------------------------------------------------*/
  public void writeRMS()
  {
    String[] strings = {"Snow", "Rain", "Sleet", "Wind", "Hail"};    
    writeStream(strings);
  }

  /*--------------------------------------------------
  * Write to record store using streams.
  *-------------------------------------------------*/  
  public void writeStream(String[] sData)
  {
    try
    {
       // Write data into an internal byte array
      ByteArrayOutputStream strmBytes = new ByteArrayOutputStream();
      
      // Write Java data types into the above byte array
      DataOutputStream strmDataType = new DataOutputStream(strmBytes);
      
      byte[] record;

      for (int i = 0; i < sData.length; i++)
      {
        // Write Java data
        strmDataType.writeUTF(sData[i]);
        
        // Clear any buffered data
        strmDataType.flush();

        // Get stream data into byte array and write record
        record = strmBytes.toByteArray();
        rs.addRecord(record, 0, record.length);      

        // Toss any data in the internal array so writes 
        // starts at beginning (of the internal array)
        strmBytes.reset();
      }
      
      strmBytes.close();
      strmDataType.close();
     
    }
    catch (Exception e)
    {
      db(e.toString());
    }
  }

  /*--------------------------------------------------
  * Read from the record store using streams
  *-------------------------------------------------*/
  public void readRMS()
  {
    try
    {
      // Careful: Make sure this is big enough!
      // Better yet, test and reallocate if necessary      
      byte[] recData = new byte[50];

      // Read from the specified byte array
      ByteArrayInputStream strmBytes = new ByteArrayInputStream(recData);

      // Read Java data types from the above byte array
      DataInputStream strmDataType = new DataInputStream(strmBytes);

      for (int i = 1; i <= rs.getNumRecords(); i++)
      {
        // Get data into the byte array
        rs.getRecord(i, recData, 0);

        // Read back the data types      
        System.out.println("Record #" + i);        
        System.out.println("UTF: " + strmDataType.readUTF());
        System.out.println("--------------------");        

        // Reset so read starts at beginning of array 
        strmBytes.reset();
      }
      
      strmBytes.close();
      strmDataType.close();
      
    }      
    catch (Exception e)
    {
      db(e.toString());
    }
  }
  
  /*--------------------------------------------------
  * Message to console for debug/errors
  *-------------------------------------------------*/
  private void db(String str)
  {
    System.err.println("Msg: " + str);
  }
}

Screen Image:


Important Points:

1)   This may be obvious, however, it bears repeating: you must write and read data types in the same order. That is, if you write out a boolean, String and integer, you must read back a boolean, String and integer, in that order. 

2)       When reading the data, make sure to allocate an array that can hold the largest record. Better yet, check the length of the data for each record and reallocate the array if necessary.

3)   This example is written for version 1.0.3 of MIDP and CLDC


Source Code:

Download (Java source code, class file and batch file)




Copyright © 2002 John W Muchow. All rights reserved.
Java, J2ME, MIDP and CLDC are Trademarks/Copyright © 1995-2002 Sun Microsystems, Inc.